﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace UpdateProgram
{
    public partial class Form1 : Form
    {
        string[] _FileSaveName;
        public Form1()
        {
            InitializeComponent();
            lblVersion.Text = "(版本号：" + Application.ProductVersion + ")";
        }

        public Form1(string[] Parameter)
        {
            InitializeComponent();
            _FileSaveName = Parameter;
            lblVersion.Text = "(版本号：" + Application.ProductVersion + ")";
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            UpdateEXEFile();
        }

        private void UpdateEXEFile()
        {
            string FileName = Path.GetDirectoryName(Application.ExecutablePath) + "\\" + _FileSaveName[0];
            try
            {
                FileVersionInfo fviSource = FileVersionInfo.GetVersionInfo(FileName);
                FileVersionInfo fviDestion = FileVersionInfo.GetVersionInfo(FileName + ".upp");
                lblUpdateInfo.Text = fviSource.FileVersion + "===>" + fviDestion.FileVersion;
                File.Copy(FileName + ".upp", FileName, true);
                progressBar1.Value = progressBar1.Maximum;
                MessageBox.Show("升级成功，将重新启动应用程序！");
                Process p = new Process();
                p.StartInfo.FileName = FileName;                
                p.Start(); //启动
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lblUpdateFile.Text = "升级文件：" + _FileSaveName;
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (progressBar1.Value <= 80)
            {
                progressBar1.Value += 10;
            }
            else
            {
                timer1.Enabled = false;
                UpdateEXEFile();
            }
        }
    }
}
